﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GreenXi.ViewModel.Utils;
using Quartz;

namespace GreenXi.Job
{
    [DisallowConcurrentExecution]
    public class SaveCurveJob : IJob
    {
        public static ITrigger GetTrigger()
        {
            return TriggerBuilder.Create().WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromMilliseconds(1000)).RepeatForever())
            .Build();
        }
        public async Task Execute(IJobExecutionContext context)
        {
           
            //根据频率往数据库写入点
            for (int i=0;i< GlobalVariables.CraftViewList.Count()&& i < GlobalVariables.ListPress.Count();i++)
            {
                //当频率达到的时候
                if (GlobalVariables.CraftViewList[i].Storage<= GlobalVariables.CraftViewList[i].SaveGatherCount&& GlobalVariables.CraftViewList[i].Status==1)
                {
                    //开始存储
                    PressUtil pressuti = new PressUtil();
                    pressuti.SaveProductionDataLogAsync(GlobalVariables.ListPress[i], GlobalVariables.CraftViewList[i].SaveCount, GlobalVariables.CraftViewList[i].Id, GlobalVariables.CraftViewList[i].CraftId, GlobalVariables.CraftViewList[i].Type.Value, GlobalVariables.CraftViewList[i].Project);
                    GlobalVariables.CraftViewList[i].SaveCount = GlobalVariables.ListPress[i].Count();

                    GlobalVariables.CraftViewList[i].SaveGatherCount = 0;
                }
                GlobalVariables.CraftViewList[i].SaveGatherCount++;
            }

            //温度表
            for (int i = 0; i < GlobalVariables.CraftViewList2.Count(); i++)
            {
                //当频率达到的时候
                if (GlobalVariables.CraftViewList2[i].Storage <= GlobalVariables.CraftViewList2[i].SaveGatherCount && GlobalVariables.CraftViewList2[i].Status == 1)
                {
                    //开始存储
                    PressUtil pressuti = new PressUtil();
                    pressuti.SaveProductionDataLogAsync2(GlobalVariables.CraftViewList2[i]);
                    GlobalVariables.CraftViewList2[i].SaveGatherCount = 0;
                }
                GlobalVariables.CraftViewList2[i].SaveGatherCount++;
            }
            for (int i = 0; i < GlobalVariables.CraftViewList3.Count(); i++)
            {
                //当频率达到的时候
                if (GlobalVariables.CraftViewList3[i].Storage <= GlobalVariables.CraftViewList3[i].SaveGatherCount && GlobalVariables.CraftViewList3[i].Status == 1)
                {
                    //开始存储
                    PressUtil pressuti = new PressUtil();
                    pressuti.SaveProductionDataLogAsync2(GlobalVariables.CraftViewList3[i]);
                    GlobalVariables.CraftViewList3[i].SaveGatherCount = 0;
                }
                GlobalVariables.CraftViewList3[i].SaveGatherCount++;
            }
        }
    }
}
